require(['grid', 'topBar', 'validator'], function () {
    var editDialog,editDialogParam,$gridParam = $('#gridParam'),$gridVrsn = $('#gridVrsn'), $addAndr=$("#addAndr"), $addIos=$("#addIos");

    //grid初始化手机参数信息
    function gridInitParam() {
    	$gridParam.grid({
            url: "/upgPhone/getUpgPhoneParamList.htm",
            width:$(window).width() - 30,
			height:100,
			showPager:false,
            colModel: [
                {name: "id",label: 'id', key: true, hidden: true},
                {name: "name",label: '名称', width: 100},
                {name: "type",label: '类型', width: 30,dict: "TEL00000"},
                {name: "packageName",label: '包名', width: 100,align:'left'},
                {name: "logo",label: 'logo', width: 100,align:'left'},
                {name: "logo2",label: 'logo@2x', width: 100, dict: "SEX00000"},
                {name: "operate", width: 50, label: '操作', title: false,align:'center',
                    formatter: 'operArea',
                    formatoptions: [{
                        text: '编辑',
                        css:'operate-edit',
                        click: function (data) {
                        	getEditDialogParam('edit','编辑').dialog('exec', 'setVal',data.id);
                        }
                    }]
                }
            ],
            multiselect: false
        });
    }
    
    //grid初始化手机版本信息
    function gridInitVrsn() {
    	$gridVrsn.grid({
            url: "/upgPhone/getUpgPhoneVrsnPage.htm",
            width:$(window).width() - 30,
			height:250,
            colModel: [
                {name: "id",label: 'id', key: true, hidden: true},
                {name: "vrsnName",label: '版本名称', width: 100},
                {name: "vrsnCode",label: '版本编号', width: 50},
                {name: "vrsnType",label: '类型', width: 30,dict: "TEL00000"},
                {name: "packageName",label: '包名', width: 100,align:'left'},
                {name: "remark",label: '升级描述', width: 100,align:'left'},
                {name: "crtTime",label: '创建时间', width: 60, dict: "SEX00000"},
                {name: "upgFile",label: '文件', width: 60, hidden: true},
                {name: "operate", width: 100, label: '操作', title: false,align:'center',
                    formatter: 'operArea',
                    formatoptions: [{
                        text: '编辑',
                        css:'operate-edit',
                        click: function (data) {
                        	 getEditDialog('edit','编辑').dialog('exec', 'setVal',data.id);
                        }
                    }, {
                        text: '删除',
                        css:'operate-delete',
                        click: function (data) {
                        	deleteObj(false,data.id);
                        }
                    },{
                        text: '下载',
                        css:'operate-download',
                        click: function (data) {
                        	if(data.upgFile != null && data.upgFile !=""){
                        		var url = "../../upgPhone/downloadFile.htm?vrsnId="+data.id;
	                        	//window.location.href=data.upgFile;
	                        	//window.location.href="/upgPhone/downloadFile.htm?vrsnId="+data.id;
                        		//downloadFile("../../"+data.upgFile);
                        		downloadFile(url);
	                        }else{
	                        	$.messager.alert("错误", "文件不存在", "error");
	                        }
                        }
                    }]
                }
            ],
            multiselect: false
        });
    }
    
  //无窗口弹出
    function downloadFile(url)  {  
		var a;
		a =window.open(url,"_blank", "width=0, height=0,status=0");
		a.document.execCommand("SaveAs");    
		//a.close();   
		//window.frames["hrong"].location.href = url;  
		//sa();  
    }  
    function sa() {  
        if(window.frames["hrong"].document.readyState!="complete")  
            setTimeout("sa()",   100);  
        else  
          window.frames["hrong"].document.execCommand('SaveAs');  
    }   
    
  //删除
    function deleteObj(isBatch,objId){
    	var objIds = objId;
    	if(isBatch){
    		objIds =  $grid.grid('getGridParam', 'selarrrow').toString();
	    	if(objIds == ""){
				$.messager.alert("提示", "请至少选择一条数据!");
				return false;
			}
    	}
    	$.messager.confirm('删除', '确定删除数据吗？', 'delete', function (r) {
	        if (r) {
				$.ajaxExt({
					async:true,
		    		url : "/upgPhone/deleteUpgPhoneVrsn.htm",
					data : {'vrsnIds' : objIds}
				}).done(function(){
					//$.messager.alert("提示","删除成功。","success");
					//reloadGridData();
					reload();
				});
	        }
    	});
	}
    
    $addIos.click(function() {
    	getEditDialog('add','新增').dialog('exec', 'reset',"TEL00001");//IOS
    });
    $addAndr.click(function() {
    	getEditDialog('add','新增').dialog('exec', 'reset',"TEL00002");//Android
    });
    
    function getEditDialogParam(flag,option) {
    	funName = flag;
        option = option || {};
        if (editDialogParam) {
        	editDialogParam.dialog('open');
            if (option.title) {
            	editDialogParam.dialog('setOption', 'title', option);
            }
        } else {
        	editDialogParam = $('<div>').dialog({
            	title: option+'版本',
                url: './upgradephone/setting/upgradePhoneParamEdit.htm',
                height: 450,
                width: 550,
                autoOpen: true,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                        	 $(this).dialog('exec', funName,function(){
                        		 reloadParam(true);
                 		    });
                        }
                    },
                    {
                        text: '重置',
                        css: 'btn-cancel',
                        click: function () {
                        	$(this).dialog('exec', 'reset');
                        }
                    }
                ]
            });
        }
        return editDialogParam;
    }

    function getEditDialog(flag,option) {
    	funName = flag;
        option = option || {};
        if (editDialog) {
            editDialog.dialog('open');
            if (option.title) {
                editDialog.dialog('setOption', 'title', option);
            }
        } else {
            editDialog = $('<div>').dialog({
            	title: option+'版本',
                url: './upgradephone/setting/upgradePhoneVrsnEdit.htm',
                height: 450,
                width: 550,
                autoOpen: true,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                        	 $(this).dialog('exec', funName,function(){
                 		    	reload(true);
                 		    });
                        }
                    },
                    {
                        text: '重置',
                        css: 'btn-cancel',
                        click: function () {
                        	$(this).dialog('exec', 'reset');
                        }
                    }
                ]
            });
        }
        return editDialog;
    }
    
    //查询grid参数
	reloadGridDataParam = function(){
		$gridParam.grid('setGridParam', {
             datatype: "JSON",
             page: 1,
             //postData: $top.getValues()
         }).trigger("reloadGrid");
	};
    //查询grid版本
	reloadGridDataVrsn = function(){
		$gridVrsn.grid('setGridParam', {
             datatype: "JSON",
             page: 1,
             //postData: $top.getValues()
         }).trigger("reloadGrid");
	};
	//新增修改后执行
	function reload(close){
		reloadGridDataVrsn();
    	close && editDialog.dialog('close');
	};
	//新增修改后执行
	function reloadParam(close){
		reloadGridDataParam();
    	close && editDialogParam.dialog('close');
	};
    //topBarInit();
    gridInitParam();
    gridInitVrsn();
    reloadGridDataParam();
    reloadGridDataVrsn();
});